﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Yunchang.ILUI
{
    /// <summary>
    /// UI的唯一View 对象
    ///   关联Prefab上的物件
    ///   王控制层传递回调事件
    /// </summary>
    public sealed class UIMono : UIBase
    {
        public enum UIType
        {
            Page,Item
        } 
        public string signId { private set; get; }

        public UIType uiType = UIType.Page;

        [HideInInspector] public List<string> keyList = new List<string>();
        [HideInInspector] public List<Object> valueList = new List<Object>();

        //TODO UIOrder Canvs 层级
        public List<UIOrder> orderList = new List<UIOrder>();
        private UIViewBase uiView = null;

        public T GetView<T>() where T : UIViewBase, new()
        {
            if (uiView == null)
            {
                uiView = new T();
                uiView.InjectMono(this); // UI 控件都手动注入
            }
            return uiView as T;
        }

        public RectTransform rectTransform
        {
            get { return transform as RectTransform; }
        }

        UIController mUICtrl;

        public bool buildViewCode = false;

        [NonSerialized]
        public bool needAwake = false;
        [NonSerialized]
        public bool needEnable = false;
       

        public void Awake()
        {
            needAwake = true;
            if (mUICtrl != null)
            {
                mUICtrl.Awake();
                needAwake = false;
            }
        }

        public void OnInit(UIController uiCtrl)
        {
            OnInit(uiCtrl, "");
        }

        public void OnInit(UIController uiCtrl, string signId)
        {
            this.mUICtrl = uiCtrl;
            this.signId = signId;
        }

        public Object GetValueByKey(string key)
        {
            if (keyList == null || valueList == null)
                return null;

            var idx = keyList.IndexOf(key);
            if (idx != -1)
                return valueList[idx];

            return null;
        }

        public T GetValueByKey<T>(string key) where T : Object
        {
            if (keyList == null || valueList == null)
                return null;
            var idx = keyList.IndexOf(key);
            if (idx != -1)
                return valueList[idx] as T;

            return null;
        }

        void Start()
        {
            if (mUICtrl != null)
            {
                mUICtrl.Start();
            }
        }

        private void OnTransformParentChanged()
        {
            if (mUICtrl != null)
            {
                mUICtrl.OnTransformParentChanged();
            }
        }

        void OnEnable()
        {
            needEnable = true;
            if (mUICtrl != null)
            {
                needEnable = false;
                mUICtrl.OnEnable();
            }
        }

        void OnDisable()
        {
            if (mUICtrl != null)
            {
                mUICtrl.OnDisable();
            }
        }

        protected override void OnDestroy()
        {
            if (mUICtrl != null)
            {
                mUICtrl.OnDestroy();
            }

            base.OnDestroy();
        }

        public void MakeData()
        {
            if (uiType == UIType.Page)
            {
                Canvas tmpCanvas = gameObject.GetComponent<Canvas>();
                if (tmpCanvas != null)
                    tmpCanvas.overrideSorting = true;
            }

            orderList.Clear();
            gameObject.GetComponentsInChildren<UIOrder>(true, orderList);

            List<Canvas> CanvasList = new List<Canvas>();
            gameObject.GetComponentsInChildren<Canvas>(true, CanvasList);

            for (int i = 0; i < CanvasList.Count; i++)
            {
                Canvas canvas = CanvasList[i];
                if (canvas.overrideSorting)
                {
                    UIOrder order = canvas.GetComponent<UIOrder>();
                    if (order == null)
                    {
                        order = canvas.gameObject.AddComponent<UIOrder>();
                        orderList.Add(order);
                    }
                }
            }

            orderList.Sort((A, B) =>
            {
                if (A.sortingOrder > B.sortingOrder) return 1;
                if (A.sortingOrder < B.sortingOrder) return -1;
                return 0;
            });
        }
    }
}
